const puppeteer =require('puppeteer')
const fs =require('fs')
const {resolve}=require('path')

const r=(path)=>resolve(__dirname,path);

// const sleep=time=>new Promise(resolve=>{
//     setTimeout(resolve,time)
// })

class Crawler{
    constructor(){
        this.maxIndex=10
        this.data=[]
        //初始化
         this.init()
    }
    async init(){
        console.log('正在启动浏览器。。。')
        this.browser=await puppeteer.launch();
       // await sleep(2000)
         this.page=await this.browser.newPage();
        for(let i=1;i<this.maxIndex;i++){
            await this.getPageInfo(i)
        }
        process.send(this.data)
        process.exit(0)
        // fs.writeFile(r('../data/movie-url.json'),JSON.stringify(this.data),(error)=>{
        //     if(error)throw error
        // })
        this.closeBrowser()
    }
    async getPageInfo(actPage){
        let page=this.page
        await page.goto(`http://www.ygdy8.net/html/gndy/dyzz/list_23_${actPage}.html`)

        //获取信息
       try {
       
           let pageUrl=await page.$$eval('.ulink',el=>{
                const data=[];
               el.forEach(element => {
                   data.push(element.href)
               });
               return data
           })
           console.log('----------开始')
           console.log('页面页码：'+actPage)
           console.log('----结束')
        
           //保存数据
           let _this=this;
           pageUrl.forEach(function (ele) {
             _this.data.push(ele)
           })
           console.log( _this.data)
       } catch (error) {
           console.log( error)
       }
     
    }
    async closeBrowser(){
        console.log("关闭浏览器。。。")
        await this.browser.close()
    }
}
//启动
new Crawler()